//
//  MYBadgeView.m
//  奇闻
//
//  Created by 似水灵修 on 14-11-21.
//  Copyright (c) 2014年 MingYueGuCheng. All rights reserved.
//

#import "MYBadgeView.h"
//badgeValue的字体
#define MYBadgeValueFont [UIFont systemFontOfSize:12]
@implementation MYBadgeView
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.userInteractionEnabled = NO;
        [self setNormalBg:@"main_badge"];
        self.titleLabel.font = MYBadgeValueFont;
        [self sizeToFit];
    }
    return self;
}
- (void)setBadgeValue:(NSString *)badgeValue
{
    _badgeValue = [badgeValue copy];
    self.hidden = ([_badgeValue isEqualToString:@"0"] || 0 == _badgeValue.length) ? YES : NO;
    [self setNormalTitle:badgeValue];
    NSMutableDictionary *textAttr = [NSMutableDictionary dictionary];
    textAttr[NSFontAttributeName] = MYBadgeValueFont;
    CGSize textSize = [badgeValue sizeWithAttributes:textAttr];
    if (textSize.width > self.width) {// 文字的尺寸大于按钮的尺寸
        [self setNormalImage:@"new_dot"];
#warning 设置图片为空时，注意要设置image 属性为nil,而非图片名为nil
        //错：[self setBackgroundImage:[UIImage imageNamed:nil] forState:UIControlStateNormal];
        [self setBackgroundImage:nil forState:UIControlStateNormal];
    }
    else {
        [self setImage:nil forState:UIControlStateNormal];
        [self setNormalBg:@"main_badge"];
        //[self setBackgroundImage:[UIImage imageNamed:nil] forState:UIControlStateNormal];
    }
}
@end
